//
//  NewsImageViewController.m
//  WXMovie
//
//  Created by apple on 16/4/1.
//  Copyright © 2016年 zic_magic. All rights reserved.
//

#import "NewsImageViewController.h"
#import "NewsImageCell.h"
#import "ImageModel.h"
#import "PhotoViewController.h"
@interface NewsImageViewController ()
{
    NSMutableArray *dataArr;
//    UIView *bgView;
//    UIImageView *imgView;
//    NSInteger index;
    
}
@end

@implementation NewsImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"图片新闻";
    self.collectionView.backgroundColor = [UIColor colorWithWhite:0.32 alpha:1];
    [self loadData];
    [self configUI];
//    [self createDetailImageView];
//    index = 0;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadData {
    
    dataArr = [NSMutableArray array];
    
    NSArray *arr = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image_list" ofType:@"json"] ] options:NSJSONReadingMutableContainers error:nil];
    
    for (NSDictionary *dic in arr) {
        
        ImageModel *model = [[ImageModel alloc] init];
        
        model.imageId = [dic[@"id"] integerValue];
        model.image = dic[@"image"];
        model.type = [dic[@"type"] integerValue];
        
        [dataArr addObject:model];
    }
    
    
    
}

- (void)configUI {
    
    self.CollectionFlowLayout.itemSize = CGSizeMake((kScreenWidth - 60) / 4 , (kScreenWidth - 60) / 4 );
    self.CollectionFlowLayout.minimumInteritemSpacing = 10;
    self.CollectionFlowLayout.minimumLineSpacing = 10;
    self.CollectionFlowLayout.sectionInset = UIEdgeInsetsMake(10, 15, 0, 15);
    
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NewsImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"NewsImageCell" forIndexPath:indexPath];
    cell.model = dataArr[indexPath.row];
//    cell.layer.cornerRadius = 5;

    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return dataArr.count;
}

//- (void)createDetailImageView {
//    
//    
//    bgView = [[UIImageView alloc] initWithFrame:self.view.bounds];
//    bgView.backgroundColor = [UIColor blackColor];
//    [self.view addSubview:bgView];
////    [self.view insertSubview:bgView aboveSubview:self.view];
//    bgView.hidden = YES;
//    
//    ImageModel *model = [[ImageModel alloc] init];
//    model = dataArr[index];
//    UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model.image]]];
//    NSInteger height = kScreenWidth / img.size.width *img.size.height;
//    imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, height)];
//    imgView.center = CGPointMake(kScreenWidth / 2, kScreenHeight / 2);
//    [imgView sd_setImageWithURL:[NSURL URLWithString:model.image]];
//    bgView.userInteractionEnabled = YES;
//    imgView.userInteractionEnabled = YES;
//    [bgView addSubview:imgView];
//    
//    
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgTapClick:)];
//    [imgView addGestureRecognizer:tap];
//    
//    UILongPressGestureRecognizer *longTap = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(imglongTapClick:)];
//    [imgView addGestureRecognizer:longTap];
//    
//    UISwipeGestureRecognizer *rightSwip = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipAction:)];
//    rightSwip.direction = UISwipeGestureRecognizerDirectionRight;
//    [imgView addGestureRecognizer:rightSwip];
//    
//    UISwipeGestureRecognizer *leftSwip = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipAction:)];
//    leftSwip.direction = UISwipeGestureRecognizerDirectionLeft;
//    [imgView addGestureRecognizer:leftSwip];
//    
//}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    index = indexPath.row;
//    ImageModel *model = [[ImageModel alloc] init];
//    model = dataArr[indexPath.row];
//    [imgView sd_setImageWithURL:[NSURL URLWithString:model.image]];
//    
//    bgView.hidden = !bgView.hidden;
//    _collectionView.hidden = !collectionView.hidden;
    
    
    PhotoViewController *photoVc = [[PhotoViewController alloc] init];
    
    photoVc.selectIndexPath = indexPath;
    
    NSMutableArray *array = [NSMutableArray array];
    
    for (ImageModel *model in dataArr) {
        
        [array addObject:model.image];
    }
    photoVc.imageURLArr = array;
    [self.navigationController pushViewController:photoVc  animated:YES];
}




- (void)leftButtonAction:(UIBarButtonItem *)leftItem {
    
    [self.navigationController popViewControllerAnimated:YES];
}

//- (void)imgTapClick:(UITapGestureRecognizer *)tap {
//    bgView.hidden = !bgView.hidden;
//    _collectionView.hidden = !_collectionView.hidden;
//    
//}

//- (void)imglongTapClick:(UILongPressGestureRecognizer *)gesture
//
//{
//    
//    UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否保存图片" preferredStyle:UIAlertControllerStyleAlert];
//    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
//    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
//        NSLog(@"保存成功");
//        
//        ImageModel *model = [[ImageModel alloc] init];
//        model = dataArr[index];
//        UIImageWriteToSavedPhotosAlbum([UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model.image]]], self, nil, nil);
//    }];
//
//    
//    [alertView addAction:cancleAction];
//    [alertView addAction:sureAction];
//    
//    [self presentViewController:alertView animated:YES completion:nil];
//
//}

//- (void)swipAction:(UISwipeGestureRecognizer *)swip {
//    
//    if (swip.direction == UISwipeGestureRecognizerDirectionLeft) {
//
//        index++;
//        if (index > dataArr.count - 1) {
//            index = 0;
//        }
//        
//    }
//    if (swip.direction == UISwipeGestureRecognizerDirectionRight ){
//
//        index--;
//        if (index < 0) {
//            index = dataArr.count - 1;
//        }
//    }
//    ImageModel *model = [[ImageModel alloc] init];
//    model = dataArr[index];
//    [imgView sd_setImageWithURL:[NSURL URLWithString:model.image]];
//    
//}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
